package com.young.common.client.threadpool.web;

import com.young.common.client.threadpool.model.EThreadPoolUpdateTypeEnum;
import com.young.common.client.threadpool.model.ThreadPoolStatus;
import com.young.common.core.concurrent.YoungThreadPoolExecutor;
import com.young.common.util.AssertUtil;
import com.young.common.util.Result;
import com.young.interfaces.log.annotation.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.stream.Collectors;

/**
 * 线程池管理web
 *
 * @author wangjy
 * @date 2021/10/16 23:24:36
 */
@Controller
@RequestMapping(value = "/threadpool/client", method = {RequestMethod.GET, RequestMethod.POST})
public class ThreadPoolManageController {

    private static final Logger logger = LoggerFactory.getLogger(ThreadPoolManageController.class);

    /**
     * 查询线程池状态列表
     *
     * @return
     */
    @Log("查询线程池状态列表")
    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        return Result.buildSuccess(YoungThreadPoolExecutor.executorList().stream().map(ThreadPoolStatus::build).collect(Collectors.toList()));
    }

    /**
     * 查询线程池状态
     *
     * @param poolName 线程池名字
     * @return
     */
    @Log("查询线程池状态")
    @RequestMapping("/get")
    @ResponseBody
    public Object get(String poolName) {
        YoungThreadPoolExecutor executor = YoungThreadPoolExecutor.get(poolName);
        return executor != null ? Result.buildSuccess(ThreadPoolStatus.build(executor)) : Result.buildFail("名字为[" + poolName + "]的线程池不存在");
    }

    /**
     * 更新线程池参数
     *
     * @param poolName 线程池名字
     * @param type     更新参数类型
     * @param value    更新后的值
     * @return
     */
    @Log("更新线程池参数")
    @RequestMapping("/update")
    @ResponseBody
    public Object updateCoreThreadSize(String poolName, String type, String value) {
        AssertUtil.notBlank(poolName, "非法的线程池");
        AssertUtil.notBlank(type, "非法的参数类型");
        AssertUtil.notBlank(value, "非法的参数值");
        YoungThreadPoolExecutor executor = YoungThreadPoolExecutor.get(poolName);
        if (executor == null) {
            return Result.buildFail("名字为[" + poolName + "]的线程池不存在");
        }
        if (EThreadPoolUpdateTypeEnum.CORE_POOL_SIZE.eq(type)) {
            int old = executor.getCorePoolSize();
            executor.setCorePoolSize(Integer.parseInt(value));
            logger.info("[更新线程池参数] 更新的线程池={},更新的属性={},旧值={},新值={}", poolName, type, old, value);
        } else if (EThreadPoolUpdateTypeEnum.MAX_POOL_SIZE.eq(type)) {
            int old = executor.getMaximumPoolSize();
            executor.setMaximumPoolSize(Integer.parseInt(value));
            logger.info("[更新线程池参数] 更新的线程池={},更新的属性={},旧值={},新值={}", poolName, type, old, value);
        }
        return Result.buildSuccess();
    }

}
